package com.gmy.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @version 1.0
 * @Description:
 * @Author 6
 * @Date 2021/8/25 10:03
 */

@Controller
public class MyController {

    @RequestMapping("/")
    public String toIndex(){
        return "index";
    }

    @RequestMapping("/toAdd")
    public String toAddxxx(){
        return  "user/add";
    }

    @RequestMapping("/toUpdate")
    public String toUpdatexxx(){
        return  "user/update";
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "user/login";
    }

    @RequestMapping("/login")
    public String login(String username , String password, Model model){


//        1.获取当前的用户
        Subject subject = SecurityUtils.getSubject();
//       2. 封装用户的登录数据
        UsernamePasswordToken userToken = new UsernamePasswordToken(username, password);

        try {
            subject.login(userToken); // 执行登录的方法  无异常 OK
            return "index";
        } catch (UnknownAccountException e) {
            model.addAttribute("msg","用户名错误");
            return "user/login";
        }catch ( IncorrectCredentialsException e) {
            model.addAttribute("msg","密码错误");
            return "user/login";
        }

    }

    @RequestMapping("/auth")
    public String author(){
        return "user/ahthentication";
    }

}
